package com.portme.client;

import java.util.ArrayList;

import com.portme.client.interfaces.Observer;
import com.portme.shared.LoginInfo;
import com.portme.shared.UserProfile;

public class StaticFactory {
	private static StaticFactory theInstance = new StaticFactory();
	
	//Important things only that NEEDs to be used all over the client side
	private LoginInfo currentUserLogin;
	private UserProfile currentProfile;
	private ArrayList<Observer> themeObservers = new ArrayList<Observer>();
	
	private StaticFactory() {}
	
	public static synchronized StaticFactory getInstance() {
		return theInstance;
	}

	public LoginInfo getCurrentUserLogin() {
		return currentUserLogin;
	}

	public UserProfile getCurrentProfile() {
		return currentProfile;
	}

	public void setCurrentProfile(UserProfile currentProfile) {
		this.currentProfile = currentProfile;
	}

	public void setCurrentUserLogin(LoginInfo currentUserLogin) {
		this.currentUserLogin = currentUserLogin;
	}
	
	public String getTheme() {
		if(this.currentProfile != null && currentProfile.getTheme() != null) {
			return currentProfile.getTheme();
		}
		return new String("default");
	}
	
	/**
	 * Not for actually setting the theme string ....<br>
	 * but to change the theme across the portfolio
	 */
	public void setTheme(String theme) {
		for(Observer o : themeObservers) {
			o.notifyMe(theme);
		}
	}
	
	public void observeTheme(Observer obs) {
		themeObservers.add(obs);
	}
	
	public boolean allowPermission() {
		LoginInfo loggedIn = StaticFactory.getInstance().getCurrentUserLogin();
		UserProfile profiled = StaticFactory.getInstance().getCurrentProfile();
		if(loggedIn != null && loggedIn.getUsername() != null) {
			if(profiled != null &&
					profiled.getUser() != null &&
					profiled.getUser().equals(loggedIn.getUsername())) {
				return true;
			}
		}
		return false;
	}
}
